{include mobile_template('header')}
<link rel="stylesheet" href="{URLROOT}/{$_G[tplurl_mobile]}/css/tuan.css" />
<div class="panel-comm">
    <div class="form-box">
        <form action="{url tuan/mobile/do/buy/rand/$_G[random]}" method="post" name="order_form">
            <input type="hidden" name="id" value="$detail[tid]">
            <!--{if $order}--><input type="hidden" name="oid" value="$order[oid]"><!--{/if}-->
            <div class="list-group list-group-insertmod">
                <div class="list-group-heading">
                    团购商品
                </div>
                <div class="list-group-item txt-small">
                    <div class="fr">
                        <span class="txt-red">&yen;{print get_numeric($detail[price])}</span> × 
                        <input class="product-order-integral" id="buynum" name="buynum" type="number" min="1" max="$buy_limit_num" name="integral" value="{if $order}$order[num]{else}1{/if}">
                    </div>
                    <h4>$detail[subject]</h4>
                    <p class="txt-comm txt-small txt-grey">
                        <span id="order-buy-message">每人最多 <span class="txt-red">{$buy_limit_num}</span> 份</span>
                    </p>
                </div>
                <!--{if $MOD[ex_pointtype] && $MOD[ex_rate] && $detail[use_ex_point] && $detail[use_ex_price] && $user->$MOD[ex_pointtype]>0}-->
                <div class="list-group-item txt-small">
                    <!--{eval 
                        $mypoint = $user->$MOD[ex_pointtype];
                        $maxpoint = (int) min($mypoint, get_numeric($detail[use_ex_price]*$MOD[ex_rate]));
                    }-->
                    <input type="hidden" name="ex_pointtype" value="$MOD[ex_pointtype]">
                    <div class="fr">
                        <input class="product-order-integral" id="ex_point" name="ex_point" type="number" min="0" max="$maxpoint" name="integral" value="{if $order}$order[ex_point]{else}0{/if}">
                    </div>
                     <h4>积分抵现金</h4>
                     <p class="txt-comm txt-small txt-grey">
                        <span id="order-buy-ex-message">你最多可用 <span class="txt-red">{$maxpoint}</span> {print:member point(point/$MOD[ex_pointtype]/keyname/unit)} {print:member point(point/$MOD[ex_pointtype])}</span>
                    </p>
                </div>
                <!--{/if}-->
                <div class="list-group-heading">
                    <!--{if $address}-->
                    <div class="fr">
                        <select id="address" onchange="insert_address(this.value);">
                            <option value=''>==选择收货地址==</option>
                            {dbres $address $val}
                            <option value="$val[name]||$val[addr]||$val[postcode]||$val[mobile]"{if $val[is_default]}default="Y"{/if}>$val[name]($val[mobile])</option>
                            {/dbres}
                        </select>
                    </div>
                    <!--{/if}-->
                    收货人信息
                </div>
                <div class="list-group-item">
                    <div class="fr">
                        <input name="mobile" value="{if $order[mobile]}$order[mobile]{else}$user->mobile{/if} ">
                    </div>
                    手机号码<span class="txt-red">*</span>
                </div>
                <!--{if $detail[sendtype]=='express'}-->
                <div class="list-group-item">
                    <div class="fr">
                        <input name="contact[linkman]" value="$order[contact][linkman]">
                    </div>
                    收货人<span class="txt-red">*</span>
                </div>
                <div class="list-group-item">
                    <div class="fr">
                        <input name="contact[postcode]" value="$order[contact][postcode]">
                    </div>
                    邮政编码
                </div>
                <div class="list-group-item">
                    <div class="fr">
                        <input name="contact[address]" value="$order[contact][address]">
                    </div>
                    收货地址<span class="txt-red">*</span>
                </div>
                <!--{/if}-->
                <div class="list-group-heading">
                    订单备注
                </div>
                 <div class="list-group-item">
                    <textarea name="contact[des]" style="width:100%;height:50px;"></textarea>
                </div>

                <div class="list-group-heading">
                    订单价格
                </div>
                <div class="list-group-borderitem">
                    <div class="list-group-item">
                        <div class="fr">&yen; <span id="order-buy-total"></span></div>
                        商品总价 +
                    </div>
                    <!--{if $mypoint}-->
                    <div class="list-group-item">
                        <div class="fr">&yen; <span id="order-buy-ex-total">0</span></div>
                        积分抵换 -
                    </div>
                    <!--{/if}-->
                    <div class="list-group-item">
                        <div class="fr">
                            <span class="txt-red">&yen;<span id="order-buy-total-t"></span></span>
                        </div>
                        应支付金额 =
                    </div>
                </div>
            </div>
            <div class="panel-comm">
                <div class="panel-body">
                    <div class="btn-group-vertical full-width">
                        <button type="submit" value="Yes" name="dosubmit" class="btn btn-mycolor">创建订单</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
var prices = new Array(2);
window.onload = function() {
    prices[0] = prices[1] = 0;
    $('#buynum').change(change_quantity);
    if($('#ex_point')[0]) {
        $('#ex_point').change(change_exchange);
    }
    change_quantity();
    {if !$order[oid]} auto_address(); {/if}
}
function auto_address() {
    var sel = $('#address');
    if(!sel[0]) return;
    sel.find('option').each(function(i) {
        if($(this).attr('default')=='Y') {
            insert_address($(this).val());
        }
    });
}
function insert_address(value) {
    if(value=='') return;
    var addr = value.split('||');
    $('[name="contact[linkman]"]').val(addr[0]);
    $('[name="contact[address]"]').val(addr[1]);
    $('[name="contact[postcode]"]').val(addr[2]);
    $('[name="mobile"]').val(addr[3]);
}
function change_quantity() {
    var limit_num = $buy_limit_num;
    var price = {print get_numeric($detail[price])};
    var num = $('#buynum').val();
    $('#order-buy-message').html('每人最多购买 '+limit_num+' 份');
    if(!is_numeric(num)) {
        $('#order-buy-message').css('color','red');
        $('#order-buy-message').html('请输入正确的数字');
    } else if(num>limit_num) {
        $('#order-buy-message').css('color','red');
    } else {
        prices[0] = accMul(num,price);
        $('#order-buy-total').html(prices[0]);
        $('#order-buy-total-t').html(accTotal());
        $('#order-buy-message').css('color','#888');
    }
}
function change_exchange() {
    var limit_point = {print (int)$maxpoint};
    var price = {print get_numeric(1/$MOD[ex_rate])};
    var point = $('#ex_point').val();
    var pointname = '{print:member point(point/$MOD[ex_pointtype])}';
    $('#order-buy-ex-message').html('您最多能使用 '+limit_point+' '+pointname);
    if(!is_numeric(point)) {
        $('#ex_point').css('color','red');
        $('#order-buy-ex-message').html('请输入正确的数字');
    } else if(point>limit_point) {
        $('#order-buy-ex-message').css('color','red');
    } else {
        prices[1] = -accMul(point,price);
        $('#order-buy-ex-total').html(prices[1]);
        $('#order-buy-total-t').html(accTotal());
        $('#order-buy-ex-message').css('color','#888');
    }
}
function accMul(arg1,arg2){
    var m=0,s1=arg1.toString(),s2=arg2.toString();  
    try{m+=s1.split(".")[1].length}catch(e){}  
    try{m+=s2.split(".")[1].length}catch(e){}  
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
function accAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    return (arg1*m+arg2*m)/m;
}
function accTotal() {
    var total = 0;
    for (var i=0; i<prices.length; i++) {
        total = accAdd(total,prices[i]);
    }
    return total;
}
</script>
{include mobile_template('footer')}